package cn.edu.bs.web.controller;

import cn.edu.bs.domain.Book;
import cn.edu.bs.domain.Type;
import cn.edu.bs.service.BookService;
import cn.edu.bs.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author bs
 */
@Controller
@RequestMapping(value = {"", "/"})
public class IndexController extends BaseController {
    @Autowired
    private BookService bookService;
    @Autowired
    private TypeService typeService;


    @GetMapping("")
    public String index(Model model) {
        List<Book> books = bookService.selectByExample(null);
        List<Type> types = typeService.selectByExample(null);
        model.addAttribute("books", books);
        model.addAttribute("booktypes", types);
        return "index";
    }

    @GetMapping("detail")
    public String detail(Model model, @RequestParam(name = "bookId") Integer bookId) {
        Book book = bookService.selectByPrimaryKey(bookId);
        model.addAttribute("book", book);
        return "detail";
    }
}
